package com.example.module_service.device

import android.app.AlertDialog
import android.os.Bundle
import android.widget.EditText
import android.widget.Toast
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_base.path.RouterPath
import com.example.lib_common.view.entity.AddPointEntity
import com.example.lib_common.view.view.BaseMvvmActivity
import com.example.module_service.R
import com.example.module_service.databinding.ActivityAddPointBinding
import com.example.module_service.viewmodel.ServerViewModel
import dagger.hilt.android.AndroidEntryPoint

//添加点位
@AndroidEntryPoint
@Route(path = RouterPath.PATH_ADD_POINT_PAGE)
class AddPointActivity : BaseMvvmActivity<ActivityAddPointBinding>() {
    override fun getLayoutId(): Int {
        return R.layout.activity_add_point
    }

    private val mViewModel: ServerViewModel by viewModels()

    override fun initViews() {
        super.initViews()

        // 设置返回按钮点击事件
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }

        // 设置提交按钮点击事件
        mDataBinding.btnCancel.setOnClickListener {
            submitPointData()
        }

        // 设置各个选择项的点击事件
        mDataBinding.tvWorkOrderType.setOnClickListener {
            // 选择所属项目
            showProjectSelectionDialog()
        }

        mDataBinding.tvDetailAddress.setOnClickListener {
            // 选择巡检类型
            showPatrolTypeSelectionDialog()
        }

        mDataBinding.etWorkOrderContent.setOnClickListener {
            // 输入点位名称
            showPointNameInputDialog()
        }

        mDataBinding.tvAppointmentTime.setOnClickListener {
            // 选择点位位置
            showPointLocationInputDialog()
        }

        mDataBinding.biaozhun.setOnClickListener {
            // 选择巡检标准
            showPatrolStandardSelectionDialog()
        }

        mDataBinding.ivAddImage.setOnClickListener {
            // 添加图片
            addImage()
        }
    }

    override fun loadData() {
        super.loadData()
        // 观察添加点位结果
        mViewModel.addPointSuccess.observe(this) { result ->
            Toast.makeText(this, "点位添加成功", Toast.LENGTH_SHORT).show()
            // 可以在这里处理添加成功后的逻辑，比如返回上一页
            finish()
        }

        // 观察错误信息
        mViewModel.mError.observe(this) { errorMsg ->
            Toast.makeText(this, "添加失败: $errorMsg", Toast.LENGTH_SHORT).show()
        }
    }

    // 提交点位数据
    private fun submitPointData() {
        // 获取表单数据
        val projectName = mDataBinding.tvWorkOrderType.text.toString()
        val patrolType = mDataBinding.tvDetailAddress.text.toString()
        val pointName = mDataBinding.etWorkOrderContent.text.toString()
        val pointLocation = mDataBinding.tvAppointmentTime.text.toString()
        val patrolStandard = mDataBinding.biaozhun.text.toString()

        // 简单验证
        if (projectName.isEmpty() || projectName == "请选择") {
            Toast.makeText(this, "请选择所属项目", Toast.LENGTH_SHORT).show()
            return
        }

        if (pointName.isEmpty()) {
            Toast.makeText(this, "请输入点位名称", Toast.LENGTH_SHORT).show()
            return
        }

        if (pointLocation.isEmpty() || pointLocation == "请选择") {
            Toast.makeText(this, "请输入点位位置", Toast.LENGTH_SHORT).show()
            return
        }

        if (patrolStandard.isEmpty() || patrolStandard == "请选择") {
            Toast.makeText(this, "请选择巡检标准", Toast.LENGTH_SHORT).show()
            return
        }

        // 创建 AddPointEntity 对象
        val addPointEntity = AddPointEntity(
            id = 0, // 新增时通常ID为0或由服务器生成
            patrolStandard = patrolStandard,
            patrolType = patrolType,
            pointImages = "", // 图片路径，如果没有可为空
            pointLocation = pointLocation,
            pointName = pointName,
            projectId = projectName // 实际项目中可能需要项目ID而非名称
        )

        // 调用 ViewModel 提交数据
        mViewModel.addPoint(addPointEntity)
    }

    // 显示项目选择对话框
    private fun showProjectSelectionDialog() {
        val projects = arrayOf("项目A", "项目B", "项目C", "智慧园区项目", "停车场管理系统")
        AlertDialog.Builder(this)
            .setTitle("选择所属项目")
            .setItems(projects) { _, which ->
                mDataBinding.tvWorkOrderType.text = projects[which]
            }
            .show()
    }

    // 显示巡检类型选择对话框
    private fun showPatrolTypeSelectionDialog() {
        val patrolTypes = arrayOf("日常巡检", "定期巡检", "专项巡检", "特殊巡检")
        AlertDialog.Builder(this)
            .setTitle("选择巡检类型")
            .setItems(patrolTypes) { _, which ->
                mDataBinding.tvDetailAddress.text = patrolTypes[which]
            }
            .show()
    }

    // 显示点位名称输入对话框
    private fun showPointNameInputDialog() {
        val editText = EditText(this)
        editText.hint = "请输入点位名称"

        AlertDialog.Builder(this)
            .setTitle("点位名称")
            .setView(editText)
            .setPositiveButton("确定") { _, _ ->
                val input = editText.text.toString().trim()
                if (input.isNotEmpty()) {
                    mDataBinding.etWorkOrderContent.text = input
                } else {
                    Toast.makeText(this, "点位名称不能为空", Toast.LENGTH_SHORT).show()
                }
            }
            .setNegativeButton("取消", null)
            .show()
    }

    // 显示点位位置输入对话框
    private fun showPointLocationInputDialog() {
        val editText = EditText(this)
        editText.hint = "请输入点位位置"

        AlertDialog.Builder(this)
            .setTitle("点位位置")
            .setView(editText)
            .setPositiveButton("确定") { _, _ ->
                val input = editText.text.toString().trim()
                if (input.isNotEmpty()) {
                    mDataBinding.tvAppointmentTime.text = input
                } else {
                    Toast.makeText(this, "点位位置不能为空", Toast.LENGTH_SHORT).show()
                }
            }
            .setNegativeButton("取消", null)
            .show()
    }

    // 显示巡检标准选择对话框
    private fun showPatrolStandardSelectionDialog() {
        val standards = arrayOf("标准巡检流程", "设备检查清单", "安全巡检标准", "环境巡检规范")
        AlertDialog.Builder(this)
            .setTitle("选择巡检标准")
            .setItems(standards) { _, which ->
                mDataBinding.biaozhun.text = standards[which]
            }
            .show()
    }

    // 添加图片
    private fun addImage() {
        // 实现添加图片的逻辑
        Toast.makeText(this, "添加图片功能待实现", Toast.LENGTH_SHORT).show()
    }

    override fun observeDataChang() {
        super.observeDataChang()
        mViewModel.addPointSuccess.observe(this) { result ->
            Toast.makeText(this, "点位添加成功", Toast.LENGTH_SHORT).show()
            // 可以发送广播或事件通知其他页面刷新数据
        }
    }
}
